package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.babystar.modules.business.dao.ShopDao;
import io.babystar.modules.business.entity.ParentEntity;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.service.ShopService;
import io.babystar.modules.sys.dao.SysUserDao;
import io.babystar.modules.sys.dao.SysUserRoleDao;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;

import io.babystar.modules.business.dao.TeacherDao;
import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.TeacherService;
import org.springframework.transaction.annotation.Transactional;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Autowired
    private ShopDao shopDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 重写分页
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageTeacher(Map<String, Object> params) {
        Page<TeacherEntity> page = new Page<>();
        Integer count = this.baseMapper.queryPageTeacherCount(params);
        page.setTotal(count);
        Integer pageNuber = Integer.parseInt(params.get("page").toString());
        Integer pageSize = Integer.parseInt(params.get("limit").toString());
        page.setCurrent(pageNuber);
        page.setSize(pageSize);
        Long current = page.getCurrent();
        Long size = page.getSize();
        Long currentStart = size * current - size;
        params.put("currentStart", currentStart);
        params.put("size", size);
        page.setRecords(this.baseMapper.queryPageTeacher(params));

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveTeacher(TeacherEntity teacher, SysUserEntity user) {
        this.baseMapper.insert(teacher);
        //生成登陆账号
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(teacher.getTeacherPhone());
        sysUserEntity.setPassword(teacher.getPassword());
        sysUserEntity.setSalt("XicP18901KbQu6mQ7AZk");
        sysUserEntity.setEmail("1@1.com");
        //默认为当前操作人的机构
        sysUserEntity.setBusinessId(user.getBusinessId());
        //3家长
        sysUserEntity.setType(3);
        sysUserEntity.setParentTeacherId(teacher.getId());
        sysUserEntity.setMobile(teacher.getTeacherPhone());
        //0:禁用   1:正常
        sysUserEntity.setStatus(1);
        sysUserEntity.setCreateUserId(user.getUserId());
        sysUserDao.insert(sysUserEntity);
    }

    @Transactional
    public void updateTeacher(TeacherEntity teacher) {
        this.baseMapper.updateById(teacher);
        ShopEntity shop = shopDao.selectById(teacher.getShopId());
        Map<String, Object> params = new HashMap<>();
        params.put("business_id", shop.getBusinessId());
        params.put("type", 3);
        params.put("parent_teacher_id", teacher.getId());
        List<SysUserEntity> userList = sysUserDao.selectByMap(params);

        if (StringUtils.isNotEmpty(teacher.getTeacherPhone())) {
            if (userList != null && userList.size() > 0) {
                SysUserEntity user = userList.get(0);
                user.setUsername(teacher.getTeacherPhone());
                sysUserDao.updateById(user);
            }
        }
    }

    @Override
    public List<TeacherEntity> getTeacherList(Map<String, Object> params) {
        return teacherDao.getTeacherList(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        for (Long id : ids) {
            TeacherEntity teacher = this.baseMapper.selectById(id);
            this.baseMapper.deleteById(teacher.getId());
            ShopEntity shop = this.shopDao.selectById(teacher.getShopId());
            Map<String, Object> p = new HashMap<>();
            //教练
            p.put("type", "3");
            p.put("username", teacher.getTeacherPhone());
            p.put("parent_teacher_id", teacher.getId());
            p.put("business_id", shop.getBusinessId());
            List<SysUserEntity> users = this.sysUserDao.selectByMap(p);
            if (users.size() > 0) {
                this.sysUserDao.deleteById(users.get(0).getUserId());
                Map<String, Object> rp = new HashMap<>();
                rp.put("user_id", users.get(0).getUserId());
                this.sysUserRoleDao.deleteByMap(rp);
            }
        }
    }

}
